package com.components.base.titlebar

import android.graphics.drawable.Drawable
import android.graphics.drawable.StateListDrawable

/**状态选择器构建器*/
class SelectorDrawable : StateListDrawable() {

    object Builder {
        /**默认状态*/
        private var mDefault: Drawable? = null

        /**焦点状态*/
        private var mFocused: Drawable? = null

        /**按下状态*/
        private var mPressed: Drawable? = null

        /**选中状态*/
        private var mChecked: Drawable? = null

        /**启用状态*/
        private var mEnabled: Drawable? = null

        /**选择状态*/
        private var mSelected: Drawable? = null

        /**光标悬浮状态（4.0新特性）*/
        private var mHovered: Drawable? = null

        fun setDefault(drawable: Drawable): Builder {
            mDefault = drawable
            return this
        }

        fun setFocused(drawable: Drawable): Builder {
            mFocused = drawable
            return this
        }

        fun setPressed(drawable: Drawable): Builder {
            mChecked = drawable
            return this
        }

        fun setChecked(drawable: Drawable): Builder {
            mChecked = drawable
            return this
        }

        fun setEnabled(drawable: Drawable): Builder {
            mEnabled = drawable
            return this
        }

        fun setSelected(drawable: Drawable): Builder {
            mSelected = drawable
            return this
        }

        fun setHovered(drawable: Drawable): Builder {
            mHovered = drawable
            return this
        }

        fun build(): SelectorDrawable {
            val selectorDrawable = SelectorDrawable()
            mPressed?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_pressed), mPressed)
            }
            mFocused?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_focused), mFocused)
            }
            mChecked?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_checked), mChecked)
            }
            mEnabled?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_enabled), mEnabled)
            }
            mSelected?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_selected), mSelected)
            }
            mHovered?.let {
                selectorDrawable.addState(intArrayOf(android.R.attr.state_hovered), mHovered)
            }
            mDefault?.let {
                selectorDrawable.addState(intArrayOf(), mDefault)
            }
            return selectorDrawable
        }
    }

}